/**
 * 
 */
package org.cnbi.utils.chart.json;

import java.util.ArrayList;
import java.util.List;

import org.cnbi.utils.chart.base.BaseChart;
import org.cnbi.utils.chart.component.Categories;
import org.cnbi.utils.chart.component.Column;
import org.cnbi.utils.chart.json.component.JsonData;
import org.cnbi.utils.chart.json.component.JsonStyles;
import org.cnbi.utils.chart.json.component.JsonDataSet;
import org.cnbi.utils.expression.ExpressionParserUtil;
import org.cnbi.utils.pojo.DynamicBean;
import org.codehaus.jackson.map.annotate.JsonSerialize;

/**
 * @company 中国经邦
 * @author Administrator
 * @date: 2014年3月14日下午5:29:03
 * @version: V1.0 多系列数据图表
 */
// @JsonRootName("chart")
public class JsonSeriesChart {

	@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
	private BaseChart chart;

	@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
	private List<Categories> categories;

	@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
	private List<JsonDataSet> dataset = new ArrayList<JsonDataSet>();

	@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
	private JsonStyles styles;

	public JsonSeriesChart() {
	}

	public BaseChart getChart() {
		return chart;
	}

	public void setChart(BaseChart chart) {
		this.chart = chart;
	}

	/**
	 * @param categories
	 * @param dateSet
	 * @param chart
	 * @param styles
	 */
	public JsonSeriesChart(String[] seriesNames, List<JsonData> datas, BaseChart chart, JsonStyles styles) {
		setMainData(seriesNames, datas);
		this.chart = chart;
		this.styles = styles;
	}

	public JsonSeriesChart(String[] seriesNames, List<JsonData> datas, BaseChart chart) {
		setMainData(seriesNames, datas);
		this.chart = chart;
	}

	public JsonSeriesChart(List<Column> seriesNames, List<DynamicBean> datas, BaseChart chart) {
		setMainData(seriesNames, datas);
		this.chart = chart;
	}

	private void setMainData(String[] seriesNames, List<JsonData> datas) {
		List<JsonData> categorie = new ArrayList<JsonData>();
		for (int i = 0, len = seriesNames.length; i < len; i++) {
			List<JsonData> datasets = new ArrayList<JsonData>();
			for (int j = 0, lj = datas.size(); j < lj; j++) {
				JsonData values = datas.get(j);
				JsonData value = new JsonData();
				value.setValue(values.getValue());
				datasets.add(value);
				if (i == 0) {
					JsonData label = new JsonData();
					label.setLabel(values.getLabel());
					categorie.add(label);
				}
			}
			dataset.add(new JsonDataSet(seriesNames[i], datasets));
		}
		categories = new ArrayList<Categories>();
		categories.add(new Categories(categorie));
	}

	private void setMainData(List<Column> seriesNames, List<DynamicBean> datas) {
		List<JsonData> categorie = new ArrayList<JsonData>();
		String pro = org.cnbi.utils.chart.Constants.INDEX + "0";
		for (int i = 0, len = seriesNames.size(); i < len; i++) {
			List<JsonData> datasets = new ArrayList<JsonData>();
			Column col = seriesNames.get(i);
			String index = col.getIndex();
			for (int j = 0, lj = datas.size(); j < lj; j++) {
				DynamicBean values = datas.get(j);
				Object temp = values.getValue(index);
				String tempValue = ExpressionParserUtil.ZEROVALUE;
				if (null != temp) {
					tempValue = temp.toString();
				}
				if (index.equals(pro)) {// 必须要有index0属性
					JsonData label = new JsonData();
					label.setLabel(tempValue);
					categorie.add(label);
				} else {
					JsonData value = new JsonData();
					value.setValue(tempValue);
					datasets.add(value);
				}
			}
			if (!index.equals(pro)) {
				JsonDataSet set = new JsonDataSet(col.getSname(), datasets);
				if (null != col.getColor()) {
					set.setColor(col.getColor());
				}
				dataset.add(set);
			}
		}
		categories = new ArrayList<Categories>();
		categories.add(new Categories(categorie));
	}

	public List<Categories> getCategories() {
		return categories;
	}

	public void setCategories(List<Categories> categories) {
		this.categories = categories;
	}

	public List<JsonDataSet> getDataset() {
		return dataset;
	}

	public void setDataset(List<JsonDataSet> dataset) {
		this.dataset = dataset;
	}

	public JsonStyles getStyles() {
		return styles;
	}

	public void setStyles(JsonStyles styles) {
		this.styles = styles;
	}

}
